<?php
/***************************************************************
*  Copyright notice
*
*  (c) 2012 Grzegorz Banka <grzegorz@grzegorzbanka.com>
*  All rights reserved
*
*  This script is part of the TYPO3 project. The TYPO3 project is
*  free software; you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation; either version 2 of the License, or
*  (at your option) any later version.
*
*  The GNU General Public License can be found at
*  http://www.gnu.org/copyleft/gpl.html.
*
*  This script is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*
*  This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
/**
 * [CLASS/FUNCTION INDEX of SCRIPT]
 *
 * Hint: use extdeveval to insert/update function index above.
 */


$LANG->includeLLFile('EXT:mt_paymenager/mod1/locallang.xml');
require_once(PATH_t3lib . 'class.t3lib_scbase.php');
$BE_USER->modAccess($MCONF,1);	// This checks permissions and exits if the users has no permission for entry.
	// DEFAULT initialization of a module [END]



/**
 * Module 'MT PayMenager' for the 'mt_paymenager' extension.
 *
 * @author	Grzegorz Banka <grzegorz@grzegorzbanka.com>
 * @package	TYPO3
 * @subpackage	tx_mtpaymenager
 */
class  tx_mtpaymenager_module1 extends t3lib_SCbase {
				var $pageinfo;

				/**
				 * Initializes the Module
				 * @return	void
				 */
				function init()	{
					global $BE_USER,$LANG,$BACK_PATH,$TCA_DESCR,$TCA,$CLIENT,$TYPO3_CONF_VARS;

					parent::init();

					/*
					if (t3lib_div::_GP('clear_all_cache'))	{
						$this->include_once[] = PATH_t3lib.'class.t3lib_tcemain.php';
					}
					*/
				}

				/**
				 * Adds items to the ->MOD_MENU array. Used for the function menu selector.
				 *
				 * @return	void
				 */
				function menuConfig()	{
					global $LANG;
					$this->MOD_MENU = Array (
						'function' => Array (
							'1' => $LANG->getLL('function1'),
							'2' => $LANG->getLL('function2'),
							'3' => $LANG->getLL('function3'),
						)
					);
					parent::menuConfig();
				}

				/**
				 * Main function of the module. Write the content to $this->content
				 * If you chose "web" as main module, you will need to consider the $this->id parameter which will contain the uid-number of the page clicked in the page tree
				 *
				 * @return	[type]		...
				 */
				function main()	{
					global $BE_USER,$LANG,$BACK_PATH,$TCA_DESCR,$TCA,$CLIENT,$TYPO3_CONF_VARS;

					// Access check!
					// The page will show only if there is a valid page and if this page may be viewed by the user
					$this->pageinfo = t3lib_BEfunc::readPageAccess($this->id,$this->perms_clause);
					$access = is_array($this->pageinfo) ? 1 : 0;
				
					if (($this->id && $access) || ($BE_USER->user['admin'] && !$this->id))	{

							// Draw the header.
						$this->doc = t3lib_div::makeInstance('mediumDoc');
						$this->doc->backPath = $BACK_PATH;
						$this->doc->form='<form action="" method="post" enctype="multipart/form-data">';

							// JavaScript
						$this->doc->JScode = '
							<script language="javascript" type="text/javascript">
								script_ended = 0;
								function jumpToUrl(URL)	{
									document.location = URL;
								}
							</script>
						';
						$this->doc->postCode='
							<script language="javascript" type="text/javascript">
								script_ended = 1;
								if (top.fsMod) top.fsMod.recentIds["web"] = 0;
							</script>
						';

						$headerSection = $this->doc->getHeader('pages', $this->pageinfo, $this->pageinfo['_thePath']) . '<br />'
							. $LANG->sL('LLL:EXT:lang/locallang_core.xml:labels.path') . ': ' . t3lib_div::fixed_lgd_cs($this->pageinfo['_thePath'], -50);

						$this->content.=$this->doc->startPage($LANG->getLL('title'));
						$this->content.=$this->doc->header($LANG->getLL('title'));
						$this->content.=$this->doc->spacer(5);
						$this->content.=$this->doc->section('',$this->doc->funcMenu($headerSection,t3lib_BEfunc::getFuncMenu($this->id,'SET[function]',$this->MOD_SETTINGS['function'],$this->MOD_MENU['function'])));
						$this->content.=$this->doc->divider(5);


						// Render content:
						$this->moduleContent();


						// ShortCut
						if ($BE_USER->mayMakeShortcut())	{
							$this->content.=$this->doc->spacer(20).$this->doc->section('',$this->doc->makeShortcutIcon('id',implode(',',array_keys($this->MOD_MENU)),$this->MCONF['name']));
						}

						$this->content.=$this->doc->spacer(10);
					} else {
							// If no access or if ID == zero

						$this->doc = t3lib_div::makeInstance('mediumDoc');
						$this->doc->backPath = $BACK_PATH;

						$this->content.=$this->doc->startPage($LANG->getLL('title'));
						$this->content.=$this->doc->header($LANG->getLL('title'));
						$this->content.=$this->doc->spacer(5);
						$this->content.=$this->doc->spacer(10);
					}
				
				}

				/**
				 * Prints out the module HTML
				 *
				 * @return	void
				 */
				function printContent()	{

					$this->content.=$this->doc->endPage();
					echo $this->content;
				}

				/**
				 * Generates the module content
				 *
				 * @return	void
				 */
				function moduleContent()	{
					switch((string)$this->MOD_SETTINGS['function'])	{
						case 1:
							if(!isset($_POST['code_insert'])) $content=$this->add_multicode(intval($_GET['id']));
							else $content=$this->insert_code();
							$this->content.=$this->doc->section('Dodawanie kodów:',$content,0,1);
						break;
						case 2:
							if(!isset($_POST['code_insert'])) $content=$this->add_multicode(intval($_GET['id']));
							else $content=$this->insert_code('multi');
							$this->content.=$this->doc->section('Dodawanie multikodów:',$content,0,1);
						break;
						case 3:
							$content='<div align=center><strong>Menu item #3...</strong></div>';
							$this->content.=$this->doc->section('Message #3:',$content,0,1);
						break;
					}
				}
				function insert_code($multi='')
				{
					$pid=intval($_GET['id']);
					if($_POST['code_insert'] && $multi=='')
					{
						$code=explode("\n",$_POST['code']);
					}else
						{
							$code=explode("\n",$_POST['code']);
							$mult='multi';
						}
					
					if($code[0]!='')
					{
						$where = 'deleted=0 AND hidden=0 AND pid='.$pid;
					
						$res =  $GLOBALS['TYPO3_DB']->exec_SELECTquery('code','tx_mtpaymenager_code',$where);
						while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res))
						{
							$iscode[]=$row['code'];
							
						}

							//print_r($iscode);
						$isadd=0;
						for($i=0; $i<count($code); $i++)
						{
							$code[$i]=trim($code[$i]);
							if(!in_array($code[$i],$iscode) && $code[$i]!='')
							{
								$add=array(
									'pid'		=>	$pid,
									'crdate'	=>	time(),
									'tstamp'	=>	time(),
									'code'		=>	$code[$i],
									'title'		=>	$mult
								);

								
								
								$GLOBALS['TYPO3_DB']->exec_INSERTquery(
										'tx_mtpaymenager_code',
										$add
								);
								 
								 
								$isadd++;
							}
						}
						return 'Dodano '.$isadd.' kodów';
					}
				}
				function add_multicode($pid)
				{	
					$where = 'deleted=0 AND hidden=0 AND pid='.$pid;					
					$res =  $GLOBALS['TYPO3_DB']->exec_SELECTquery('title,price,idseller','tx_mtpaymenager_setting',$where,'','',1);
					while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res))
					{
						$label='<tr><td>'.$row['title'].'</td><td>'.$row['price'].'</td><td>'.$row['idseller'].'</td></tr>';
					}
					if($label!='')
					{
						return '<table>
						<tr><td>Nazwa</td><td>Cena</td><td>ID</td></tr>
						'.$label.'
						</table>
						<textarea name="code" cols="50" rows="10"></textarea><br />
						<input name="code_insert" type="submit" value="Dodaj kody" />
						
						';
					}else
						{
							return '<b>Brak ustawień dla tego folderu</b>';							
						}
				}
				
		}



if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/mt_paymenager/mod1/index.php'])	{
	include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/mt_paymenager/mod1/index.php']);
}




// Make instance:
$SOBE = t3lib_div::makeInstance('tx_mtpaymenager_module1');
$SOBE->init();

// Include files?
foreach($SOBE->include_once as $INC_FILE)	include_once($INC_FILE);

$SOBE->main();
$SOBE->printContent();

?>